package com.swsc.solarverse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swsc.solarverse.dao.entity.StationModuleDO;
import com.swsc.solarverse.dao.mapper.StationModuleMapper;
import com.swsc.solarverse.service.IStationModuleService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2022-03-20
 */
@Service
public class StationModuleServiceImpl extends ServiceImpl<StationModuleMapper, StationModuleDO> implements IStationModuleService {

    @Override
    public List<StationModuleDO> listAllByOrganNo(Collection<Integer> organNoIn) {
        return baseMapper.selectList(Wrappers.lambdaQuery(StationModuleDO.class).in(StationModuleDO::getOrganNo, organNoIn));
    }

    @Override
    public StationModuleDO getStationModule(Integer tenantNo, Integer organNo, String model) {
        return baseMapper.selectOne(Wrappers.lambdaQuery(StationModuleDO.class)
                .eq(null != tenantNo, StationModuleDO::getTenantNo, tenantNo)
                .eq(StationModuleDO::getOrganNo, organNo)
                .eq(StationModuleDO::getModel, model));
    }

    @Override
    public List<StationModuleDO> listStationModules(Integer tenantNo, Integer organNo, Collection<String> models) {
        return baseMapper.selectList(Wrappers.lambdaQuery(StationModuleDO.class)
                .eq(null != tenantNo, StationModuleDO::getTenantNo, tenantNo)
                .eq(StationModuleDO::getOrganNo, organNo)
                .in(StationModuleDO::getModel, models));
    }

    @Override
    public List<StationModuleDO> listStationModules(Integer tenantNo, Integer organNo) {
        return baseMapper.selectList(Wrappers.lambdaQuery(StationModuleDO.class)
                .eq(null != tenantNo, StationModuleDO::getTenantNo, tenantNo)
                .eq(StationModuleDO::getOrganNo, organNo)
        );
    }

    @Override
    public Map<String, StationModuleDO> getStationModuleMap(Integer tenantNo, Integer organNo) {
        List<StationModuleDO> moduleDos = baseMapper.selectList(Wrappers.lambdaQuery(StationModuleDO.class)
                .eq(null != tenantNo, StationModuleDO::getTenantNo, tenantNo)
                .eq(StationModuleDO::getOrganNo, organNo));
        if (CollectionUtils.isNotEmpty(moduleDos)) {
            return moduleDos.stream().collect(Collectors.toMap(StationModuleDO::getModel, Function.identity(), (k1, k2) -> k2));
        }
        return null;
    }

    @Override
    public Map<Integer, Float> getStationModuleAvgPower(List<Integer> organNoIn) {
        QueryWrapper<StationModuleDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("tenant_no, organ_no, (SUM(power * num) / SUM(num)) as power");
        queryWrapper.lambda().in(StationModuleDO::getOrganNo, organNoIn);
        queryWrapper.groupBy("tenant_no, organ_no");
        List<StationModuleDO> stationModuleDos = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(stationModuleDos)) {
            return stationModuleDos.stream().filter(md -> Objects.nonNull(md.getOrganNo()) && Objects.nonNull(md.getPower())).collect(Collectors.toMap(StationModuleDO::getOrganNo, StationModuleDO::getPower, (k1, k2) -> k2));
        }
        return Collections.EMPTY_MAP;
    }
}
